package com.niit.dao;

import com.niit.pojo.StudentTable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.*;

/**
 * @author 杨铭
 * 2022/2/7,20:47
 */
public class displayStudent {
    Configuration conf;

    public displayStudent() {
        HbaseConnection hbaseConnection = new HbaseConnection();
        Configuration conf = hbaseConnection.connect();
        this.conf = conf;
    }

    /**
     * 实现分页查询
     * 客户端需要记录上一次返回的row的Key值
     **/
    public List<StudentTable> scanByPage(String endRow) throws IOException {
        Filter filter = new PageFilter(10);
        byte[] lastRow = Bytes.toBytes(endRow);
        HTable hTable = new HTable(conf, "Student_Table");
        Scan scan = new Scan();
        scan.setFilter(filter);
        List<StudentTable> list = new ArrayList<>();
        if (lastRow != null) {
            scan.setStartRow(lastRow);
        }
        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result : scanner) {
            String studentID = Bytes.toString(result.getRow());
            String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("Name")));
            String dateOfEnrollment = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("DateOfEnrollment")));
            String contactNumber = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("ContactNumber")));
            String semester = Bytes.toString(result.getValue(Bytes.toBytes("status"), Bytes.toBytes("Semester")));
            String studentStatus = Bytes.toString(result.getValue(Bytes.toBytes("status"), Bytes.toBytes("StudentStatus")));
            StudentTable studentTable = new StudentTable(studentID, name, dateOfEnrollment, contactNumber, semester, studentStatus);
            list.add(studentTable);
        }
        scanner.close();
//        System.out.println("list======="+list);
        return list;
    }

}
